<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<script type="text/javascript">
     $(function(){
         $('#dg').datagrid({
             url: '${pageContext.request.contextPath}/manage/queryManagerList',
             toolbar:'manage_tool',
             columns: [
                 [{
                     field: 'username',
                     title: '用户名称',
                     width: 100
                 }, {
                     field: 'phone',
                     title: '手机号',
                     width: 100
                 }, {
                     field: 'age',
                     title: '年龄',
                     width: 100
                 }, {
                     field: 'nick',
                     title: '昵称',
                     width: 100
                 }]
             ],
             onLoadSuccess: function(data) {           //成功后的回调函数
                 if (data.total == 0) {
                     var body = $(this).data().datagrid.dc.body2;
                     body.find('table tbody').append('<tr><td width="' + body.width() + '" style="height: 25px; text-align: center;">无查询数据</td></tr>');
                 }
             },
             width:1200,
             height: 400,
             fitColumns: true,
             pagination: true,                   //支持分页
             pageSize: 20,                        //每页20条数据
             pageList:[10, 20, 50, 100]     //pageSize可选项，默认是[10,20,30,40,50]
         });

         <%-------------1、新增------------------%>
         $('#manage_add').dialog({
             modal:true,
             closed:true,
             width:300,
             title:'新增管理员',
             buttons:[
                 {
                     text:'提交',
                     handler:function(){
                         if($("#manage_add").form('validate')){
                             $.ajax({
                                 url:'${pageContext.request.contextPath}/manage/add',
                                 type:'post',
                                 data:{
                                     username:$('input[name="username"]').val(),
                                     password:$('input[name="password"]').val(),
                                     phone:$('input[name="phone"]').val(),
                                     age:$('input[name="age"]').val(),
                                     nick:$('input[name="nick"]').val()
                                 },
                                 beforeSend:function(){
                                     $.messager.progress({
                                         text:'正在新增中...'
                                     });
                                 },
                                 success:function(data){
                                     $.messager.progress('close');
                                     if(data > 0){
                                         $.messager.show({
                                             title:'提示',
                                             msg:'新增成功'
                                         });
                                         $('#manage_add').dialog('close').form('reset');
                                         $('#dg').datagrid('reload');
                                     }else{
                                         $.messager.alert('新增失败','未知错误','warning');
                                     }
                                 }
                             });
                         }
                     }
                 },{
                     text:'取消',
                     handler:function(){
                         $('#manage_add').dialog('close').form('reset');
                     }
                 }]
         });

         <%-------------2、修改------------------%>
         $('#manage_edit').dialog({
             modal:true,
             closed:true,
             width:300,
             title:'修改管理员',
             buttons:[
                 {
                     text:'提交',
                     handler:function(){
                         if($("#manage_edit").form('validate')){
                             $.ajax({
                                 url:'${pageContext.request.contextPath}/manage/update',
                                 type:'post',
                                 data:{
                                     id:$('#id').val(),
                                     username:$('input[name="username_edit"]').val(),
                                     phone:$('input[name="phone_edit"]').val(),
                                     age:$('input[name="age_edit"]').val(),
                                     nick:$('input[name="nick_edit"]').val()
                                 },
                                 beforeSend:function(){
                                     $.messager.progress({
                                         text:'正在修改中...'
                                     });
                                 },
                                 success:function(data){
                                     $.messager.progress('close');
                                     if(data > 0){
                                         $.messager.show({
                                             title:'提示',
                                             msg:'修改成功'
                                         });
                                         $('#manage_edit').dialog('close').form('reset');
                                         $('#dg').datagrid('reload');
                                     }else{
                                         $.messager.alert('修改失败','未知错误','warning');
                                     }
                                 }
                             });
                         }
                     }
                 },{
                     text:'取消',
                     handler:function(){
                         $('#manage_edit').dialog('close').form('reset');
                     }
                 }]
         });
     });



     <%-------------1、新增------------------%>
     //管理员名称
     $('input[name="username"]').validatebox({
         required:true,
         validType:length[2,20],
         missingMessage:'请输入管理员名称',
         invalidMessage:'管理员名称在2到20位之间'
     });

     //管理员密码
     $('input[name="password"]').validatebox({
         required:true,
         validType:length[6,30],
         missingMessage:'请输入管理员密码',
         invalidMessage:'管理员名称在6到30位之间'
     });

     $('input[name="auth"]').combotree({
         url:'${pageContext.request.contextPath}/role/getRoles',
         lines:true,
         required:true,
         multiple:true,
         checkbox:true,
         onlyLeafCheck:true,
         onLoadSuccess:function(node,data){
             var _this = this;
             if(data){
                 $(data).each(function(index,value){
                     if(this.state == 'closed'){
                         $(_this).tree('expandAll');
                     }
                 });
             }
         }
     });

     <%-------------2、修改------------------%>
     //管理员名称
     $('input[name="username_edit"]').validatebox({
         required:true,
         validType:length[2,20],
         missingMessage:'请输入管理员名称',
         invalidMessage:'管理员名称在2到20位之间'
     });

     //管理员密码
     $('input[name="password_edit"]').validatebox({
         required:true,
         validType:length[6,30],
         missingMessage:'请输入管理员密码',
         invalidMessage:'管理员名称在6到30位之间'
     });

     $('input[name="auth_edit"]').combotree({
         url:'${pageContext.request.contextPath}/role/getRoles',
         lines:true,
         required:true,
         multiple:true,
         checkbox:true,
         onlyLeafCheck:true,
         onLoadSuccess:function(node,data){
             var _this = this;
             if(data){
                 $(data).each(function(index,value){
                     if(this.state == 'closed'){
                         $(_this).tree('expandAll');
                     }
                 });
             }
         }
     });

     <%-------------点击添加、修改、删除------------------%>
     manage_tool={
       add :function(){
          $("#manage_add").dialog("open");
          $("input[name='username']").focus();
       },
       edit:function(){
           var rows = $("#dg").datagrid('getSelections');
           if(rows.length > 1){
               $.messager.alert('警告操作','最多只能选择一条记录操作','warning');
           }else if(rows.length == 1){
               $('#id').val(rows[0].id);
               $('input[name="username_edit"]').val(rows[0].username);
               $('input[name="phone_edit"]').val(rows[0].phone);
               $('input[name="age_edit"]').val(rows[0].age);
               $('input[name="nick_edit"]').val(rows[0].nick);
               $('#manage_edit').dialog('open');
           }else if(rows.length < 1){
               $.messager.alert('警告操作','至少需要选择一条记录操作','warning');
           }
       },
       remove:function(){
           var rows = $("#dg").datagrid('getSelections');
           if(rows.length > 0){
               $.messager.confirm('确定操作','您要删除选择的记录吗？',function(flag){
                   if(flag){
                       var ids = [];
                       for(var i=0;i<rows.length;i++){
                           ids.push(rows[i].id);
                       }
                       $.ajax({
                           url:'${pageContext.request.contextPath}/manage/delete',
                           type:'post',
                           data:{
                               ids:ids.join(',')
                           },
                           beforeSend:function(){
                               $.messager.progress({
                                   text:'正在删除中...'
                               });
                           },
                           success:function(data){
                               if(data){
                                   $.messager.progress('close');
                                   $.messager.show({
                                       title:'提示',
                                       msg:data+'个记录被成功删除'
                                   });
                                   $('#dg').datagrid('reload');
                               }
                           }
                       });
                    }
               });
           }else{
               $.messager.alert('提示','请选择删除的记录','info');
           }
       }
    }
</script>

<style type="text/css">
   .dialog-button{
       text-align: center;
   }
</style>

<div id="head" style="padding: 10px 10px">
    <div style="padding: 5px 0px 5px 0px;color: #333;width: 300px;float: left;">
        查询帐号：<input type="text" class="textbox" id="username" style="width: 110px"/>
        &nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="obj.search();">查询</a>
    </div>

    <div id="manage_tool" style="padding: 0px;width: 300px;float: right">
          <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="manage_tool.add();">添加</a>
          <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="manage_tool.edit();">修改</a>
          <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="manage_tool.remove();">删除</a>
          <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" id="save" onclick="obj.save();">保存</a>
          <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" id="redo" onclick="obj.redo();">撤销</a>
    </div>
</div>
<div id="dg"></div>

<%-------------1、新增------------------%>
<form id="manage_add">
    <p>管理员帐号：<input type="text" name="username" class="textbox" style="width: 200px"></p>
    <p>管理员密码：<input type="text" name="password" class="textbox" style="width: 200px"></p>
    <p>手机号：<input type="text" name="phone" class="textbox" style="width: 200px"></p>
    <p>年龄：<input type="text" name="age" class="textbox" style="width: 200px"></p>
    <p>昵称：<input type="text" name="nick" class="textbox" style="width: 200px"></p>
    <%--<p>分配权限：<input  name="auth" class="textbox" style="width: 200px"></p>--%>
</form>

<%-------------2、修改------------------%>
<form id="manage_edit">
    <input type="hidden" id="id" name="id">
    <p>管理员帐号：<input type="text" name="username_edit" disabled="true" class="textbox" style="width: 200px"></p>
    <p>手机号：<input type="text" name="phone_edit" class="textbox" style="width: 200px"></p>
    <p>年龄：<input type="text" name="age_edit" class="textbox" style="width: 200px"></p>
    <p>昵称：<input type="text" name="nick_edit" class="textbox" style="width: 200px"></p>
</form>

